<?php
namespace Home\controller;

use Think\Controller;
header("content-type:text/html;charset=utf-8");
class IndexController extends Controller
{
	public function __construct()
	{
		parent::__construct();
		// 如果是已经登录,判断该用户今天第几次登录
		if (!empty($_SESSION)) {
			$user = M('user');
			$where['uid'] = $_SESSION['uid'];
			$userInfo = $user->where($where)->find();
			// 得到签到时间是否是今天
			$time1 = date('Y-m-d',time());
			$time2 = date('Y-m-d',$userInfo['signtime']);
			// 如果是今天就向页面输出 1 显示为已签到
			if ($time1 == $time2) {
				$this->assign('tag',1);
			} else {
				$this->assign('tag',0);
			}
		}
		//查询友情链接
		$youqing=M('youqing')->select();
		$this->assign('youqing',$youqing);
	}
	
	public function bbsIndex()
	{
		// 天气接口
		// 转换编码UTF-8至GBK
		$city = iconv('UTF-8','GBK','上海');
		$url = 'http://php.weather.sina.com.cn/xml.php?city='.$city.'&password=DJOYnieT8234jlsK&day=0';
		//CURL的初始化
	    $curl = curl_init();
	    // var_dump($curl);

	    //CURL配置
	    curl_setopt($curl,CURLOPT_URL,$url);
	    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
	    //curl执行
	    $data = curl_exec($curl);

		//关闭curl
	    curl_close($curl);

	    //处理xml 返回对象
	    $obj = simplexml_load_string($data);
	    $this->assign('obj',$obj);
	    // 显示所有帖子
	    $data = $this->BBSselect();
		$this->assign('data',$data);
		$this->display('BBSIndex/bbsindex');
	}
	
	public function index()
	{
		//实例化轮播图
		$lbt = M('lbt');
		$data = $lbt->select();
		//查询一级分类
		$type = M('type');
		$fatherMap['fid']= '0';
		$dataType = $type->where($fatherMap)->select();
		$this->assign('dataType',$dataType);
		$this->assign('list',$data);
		$this->display('Index/index');
	}

	//获取健康咨询API
	public function HealthNews()
	{

		$ch = curl_init();
    	$url = 'http://apis.baidu.com/tngou/info/news?id=0&classify=0&rows=20';
    	$header = array(
        	'apikey: c15cfb717cd9046dca16bc68ce6a9ae9',
    	);
    	// 添加apikey到header
    	curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	// 执行HTTP请求
    	curl_setopt($ch , CURLOPT_URL , $url);
    	$res = curl_exec($ch);
   		// var_dump(json_decode($res));
   		$res = json_decode($res);
   		$res = $res->tngou;
   		// var_dump($res);
   		foreach ($res as $key => $value) {
   			$data[$key]['message'] = $value->message;
   			$data[$key]['description'] = $value->description;
   			$data[$key]['img'] = $value->img;
   			$data[$key]['title'] = $value->title;
   			// dump($value->time);die;
   			$data[$key]['time'] = $value->time;
   			$data[$key]['keywords'] = $value->keywords;
   		}
   		$this->assign('HN',$data);
   		$this->display();
	}

	//查询所有帖子的函数
	public function BBSselect($a = 0,$b = 15)
	{
		
		$model = M('invitation');
		$rep = M('comment');
		$count = $model->count();
		$data = $model->field(array('user.username','user.userimage','type.tname','invitation.intitle','user.uid','invitation.inid','invitation.intime','invitation.inpv','type.tid'))->table(array('bbs_user'=>'user','bbs_type'=>'type','bbs_invitation'=>'invitation'))->where('invitation.pid=type.tid and invitation.uid=user.uid')->select();
		$time = time();//生成当前时间
		//构造二维数组      查询出每个帖子下面的最新一条评论信息
		foreach ($data as $key => $value) {
			$data[$key]['comment'] = $rep->where("inid = {$value['inid']}")->order('ctime desc')->limit(1)->select();
			$data[$key]['vtime'] = $this->ViewTime($time,$data[$key]['comment']);
			//计算评论数
			$data[$key]['count'] = $rep->where("inid = {$value['inid']}")->select();
			$data[$key]['count'] = count($data[$key]['count']);
			//增加一个用于排序的数组
			if(!empty($data[$key]['vtime'])){
				$order[$key] = $data[$key]['comment'][0]['ctime'];
			}else{
				$order[$key] = $data[$key]['intime'];
			}
		}
		//排序
		array_multisort($order, SORT_DESC, $data); 
		//数组截取
		$data = array_slice($data, $a, $b, true);
		return $data;
	}
	//计算回帖时间距今时间的函数
	public function ViewTime($a,$b){
			if(!empty($b)){
				$ctime = ($a - $b[0]['ctime'])/60;//计算时间差 分钟为单位
				$ctime = (int)$ctime;
			if($ctime<1){
				$vtime = '刚刚';//$vtime用于传值于前端显示的字符型变量，$ctime用于计算的int型变量
			}else{
				$vtime = $ctime.'分钟前';
			}
			//如果大于60分钟
			if($ctime>60){
				$ctime = $ctime/60;//小时为单位
				$ctime = (int)$ctime;
				$vtime = $ctime.'小时前';
				//如果大于24小时
				if($ctime>24){
					$ctime = (int)$ctime;
					$ctime = $ctime/24;//天 为单位
					$ctime = (int)$ctime;
					$vtime = $ctime.'天前';
					//大于7天 直接显示年月日
					if($ctime>7){
						$vtime = date("Y年m月d日",$b[0]['ctime']);
					}
				}
			}
		return $vtime;
		}
	}
} 